<template>
  <div
    ref="main"
    :class="className"
    :style="{ height: height, width: width }"
  ></div>
</template>

<script>
import * as echarts from 'echarts'
// 图表自适应
import resize from './mixins/resize'
require('echarts/theme/macarons')
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '400px'
    },
    legendData: {
      type: Array,
      default: () => ["前端", "Java", "移动端", "大数据", "人工智能", "区块链"]
    },
    seriesData: { // 柱状数据
      type: Array,
      default: () => [
        { value: 335, name: '前端' },
        { value: 310, name: 'Java' },
        { value: 234, name: '移动端' },
        { value: 135, name: '大数据' },
        { value: 1548, name: '人工智能' },
        { value: 1548, name: '区块链' }
      ]
    }
  },
  data () {
    return {
      chart: null
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.initCharts()
    })
  },
  beforeDestroy () {
    // 如果为Null 直接return
    if (!this.chart) return
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initCharts () {
      // 初始化图表
      this.chart = echarts.init(this.$refs.main, 'macarons')
      // 图表数据
      this.chart.setOption({
        title: {
          text: '各技术频道文章统计', // 主标题
          subtext: 'Fake Data',
          left: 'center'
        },
        tooltip: { //鼠标放上去显示的格式
          trigger: 'item'
        },
        legend: { // 图例
          orient: 'vertical', // 垂直
          left: 'left', // 左侧
          data: this.legendData
        },
        series: [ //图数据
          {
            name: '统计内容',
            type: 'pie',
            radius: '50%',
            data: this.seriesData,
            emphasis: {
              itemStyle: {
                shadowBlur: 10, // 阴影模糊大小
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      })
    }
  },
}
</script>

<style>
</style>